package utils

import (
	"github.com/360EntSecGroup-Skylar/excelize"
	"strconv"
	"strings"
)

// ReadFile 读取excel中第一列的数据，并返回切片
func ReadFile(filename string) ([]string, error) {
	file, err := excelize.OpenFile(filename)
	if err != nil {
		return []string{}, err
	}
	res := []string{}
	rows := file.GetRows("Sheet1")
	for _, v := range rows {
		res = append(res, strings.TrimSpace(v[0]))
	}
	return res, nil
}

func ExportFile(filename string, list []string) {
	file := excelize.NewFile()
	for k, v := range list {
		file.SetCellValue("Sheet1", "A" + strconv.Itoa(k + 1), v)
	}
	// 直接设置一行
	// file.SetSheetRow("Sheet1", "A1", &list);
	err := file.SaveAs(filename)
	if err != nil {
		panic(nil)
	}
}